<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../rest.css">
    <script src="../rest.js"></script>
    <style>
        span,
        .list li {
            border: 1px red solid;
            margin: 20px;
            width: 300px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            display: block;
        }
    </style>
</head>

<body>
    <button class="btn">新增</button> <button class="btn">删除</button>
    <ul class="list">
        <li class="first">01</li>
        <li class="lii">02</li>
        <li class="lii">03</li>
        <span>这个是一个突兀的span标签</span>
        <li class="lii">04</li>
        <li class="lii">05</li>
        <li class="lii">06</li>
    </ul>
</body>
<script>
    var btn1 = document.getElementsByClassName('btn')[0]
    var btn2 = document.getElementsByClassName('btn')[1]
    var list = document.getElementsByClassName('list')[0]
    var liList = list.getElementsByTagName("li")


    list.onclick = function (e) {
        var e = e || window.event
        var target = e.target
        var index = Array.from(liList).indexOf(target)
        if (target.className == 'lii') {
            console.log('这个是原本的li标签', target.innerHTML, index)
        }
        else if (target.classList.contains('newLi')) {
            console.log('这个是新生成的li', target.innerHTML, index)
        }
        else if (target.className == 'first') {
            console.log('这个是第一个li')
        }
        else if (target.tagName == 'SPAN') {
            console.log(target.innerHTML)
        }
    }



    btn1.onclick = function () {
        var li = document.createElement('li')
        li.innerHTML = '我可是一个新增的li哦'
        li.setAttribute('class', 'newLi')
        li.style.background = randColor()
        list.append(li)
    }

    btn2.onclick = function () {
        list.lastElementChild.remove()
    }

</script>

</html>